import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../../EcyRouter.dart';
import '../../../constant/EcyEventConsnt.dart';
import '../../../ecycommon/EcyCommonWidgets.dart';
import '../../../ecycommon/EcyEventBus.dart';
import '../../../ecycommon/utils/EcyUserUtil.dart';
import '../../../user/model/EcyUserModel.dart';

class EcyAccountSecurityPage extends EcyCommonBaseWidget {
  EcyAccountSecurityPage({required super.key});

  @override
  _EcyAccountSecurityPageState createState() {
    return _EcyAccountSecurityPageState(this);
  }
}

class _EcyAccountSecurityPageState extends EcyCommonBaseWidgetState {
  late EcyCommonBaseWidget controller;

  _EcyAccountSecurityPageState(EcyCommonBaseWidget vc) {
    controller = vc;
  }

  EcyUserModel _user = EcyUserModel("");

  @override
  void initState() {
    super.initState();

    reloadData();

    registerEvents();
  }

  void registerEvents() {
    EcyEventBus().on(EcyEventConst.kLogined, (arg) {
      reloadData();
    });
    EcyEventBus().on(EcyEventConst.kEditUser, (arg) {
      reloadData();
    });
  }

  reloadData() async {
    EcyUserModel? user = await EcyUserUtil.getLoginedUserInfo();
    if (user != null) {
      setState(() {
        _user = user;
      });
    } else {
      setState(() {
        _user = EcyUserModel("");
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF5F5F5),
      appBar: AppBar(
        //导航栏
        title: const Text('账号与安全', style: TextStyle(fontSize: 18)),
        centerTitle: true,
        backgroundColor: Colors.white,
      ),
      body: Padding(
          padding: const EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Card(
                color: Colors.white,
                elevation: 0,
                margin: const EdgeInsets.only(bottom: 2),
                child: ListTile(
                  title: const Text(
                    '昵称',
                    style: TextStyle(fontSize: 18),
                  ),
                  trailing: Text(
                    _user.nickname ?? "",
                    style: const TextStyle(fontSize: 18, color: Colors.grey),
                  ),
                  splashColor: Colors.transparent,
                  onTap: () {
                    EcyRouter.pushNoParams(context, EcyRouter.personInfoPage);
                  },
                ),
              ),
              Card(
                color: Colors.white,
                elevation: 0,
                margin: const EdgeInsets.only(bottom: 2),
                child: ListTile(
                  title: const Text(
                    "手机号",
                    style: TextStyle(fontSize: 18),
                  ),
                  trailing: RichText(
                    text: TextSpan(children: [
                      TextSpan(
                          text: _user.username,
                          style: const TextStyle(
                              color: Colors.grey, fontSize: 18)),
                      // const WidgetSpan(
                      //     alignment: PlaceholderAlignment.top,
                      //     child: Icon(Icons.arrow_forward_ios, size: 14)
                      // ),
                    ]),
                  ),
                  splashColor: Colors.transparent,
                  onTap: () {},
                ),
              ),
              Card(
                color: Colors.white,
                elevation: 0,
                margin: const EdgeInsets.only(bottom: 2),
                child: ListTile(
                  title: const Text(
                    '修改登录密码',
                    style: TextStyle(fontSize: 18),
                  ),
                  trailing: RichText(
                      text: const TextSpan(children: [
                    WidgetSpan(
                        alignment: PlaceholderAlignment.top,
                        child: Icon(Icons.arrow_forward_ios, size: 14)),
                  ])),
                  splashColor: Colors.transparent,
                  onTap: () {
                    EcyRouter.pushWithNeedLogin(
                        context, EcyRouter.resetPasswordPage, {});
                  },
                ),
              ),
              Card(
                color: Colors.white,
                elevation: 0,
                margin: const EdgeInsets.only(bottom: 2),
                child: ListTile(
                  title: const Text(
                    '账号注销',
                    style: TextStyle(fontSize: 18),
                  ),
                  trailing: RichText(
                      text: const TextSpan(children: [
                        WidgetSpan(
                            alignment: PlaceholderAlignment.top,
                            child: Icon(Icons.arrow_forward_ios, size: 14)),
                      ])),
                  splashColor: Colors.transparent,
                  onTap: () {
                    EcyRouter.pushWithNeedLogin(
                        context, EcyRouter.logOffPage, {});
                  },
                ),
              ),
              // Card(
              //   color: Colors.white,
              //   elevation: 0,
              //   margin: const EdgeInsets.only(bottom: 2),
              //   child: ListTile(
              //     title: const Text(
              //       '微信账号',
              //       style: TextStyle(fontSize: 18),
              //     ),
              //     trailing: RichText(
              //         text: const TextSpan(
              //           children: [
              //             TextSpan(text: '微信账号', style: TextStyle(color: Colors.grey, fontSize: 18)),
              //             WidgetSpan(
              //               alignment: PlaceholderAlignment.top,
              //               child: Icon(Icons.arrow_forward_ios, size: 14)
              //             ),
              //           ]
              //         )
              //     ),
              //     splashColor: Colors.transparent,
              //     onTap: () {},
              //   ),
              // ),
            ],
          )),
    );
  }
}
